package xyz.csmcool.imserver.mqHander;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import xyz.csmcool.imserver.config.HttpAuthHandler;
import xyz.csmcool.imserver.config.PrivateChatHandler;


/**
 * @author 什锦
 * @Package xyz.csmcool.imchat.service.impl
 * @since 2022/8/14 0:39
 */

@Component
public class PrivateChatReceiver {


    private static final long RETRY_INTERVAL = 2;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "privatechat", durable = "true"),
            exchange = @Exchange(value = "broadcast2", durable = "true", type = "fanout",
                    ignoreDeclarationExceptions = "true"), key = "im.privatechat"))
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        try {
            //手工ACK
            MessageHeaders headers = message.getHeaders();
            Integer uid = (Integer) headers.get("uid");
            Integer sid = (Integer) headers.get("sid");
            System.out.println(uid);
            PrivateChatHandler.onMessage(message.getPayload().toString(), uid,sid);

            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicAck(deliveryTag, false);
        }
    }
}
